using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Net;
using System.Collections;

namespace BlogBackUp
{
    public class RefFile
    {
        public String sourceUrl;     // reference file's source url in the html
        public String sourceFullUrl; // reference file's destination full url
        public String destPath;      // reference file's local destination full path
        public String fileName;      // reference file's file name that contains extention
        public String ext;           // reference file's extention        
    }

    public class DownloadUtils
    {
        public static  bool IsNeedFixPathAndDownload(String ext)
        {
            if (CommonUtils.IsImg(ext)
               || ext.ToLower() == ".css")
            {
                return true;
            }
            return false;
        }

        public static void DeleteNoNeededRefFile(ref ArrayList refFileList, String htmlFile)
        {
            ArrayList tempRefFilelist = new ArrayList();
            for (int i = 0; i < refFileList.Count; i++)
            {
                RefFile refFile = (RefFile)refFileList[i];
                if (IsNeedFixPathAndDownload(refFile.ext))
                {
                    if (htmlFile.IndexOf(refFile.sourceUrl) >= 0)
                    {
                       // for write, so must use imgsFileList[i]                        
                        tempRefFilelist.Add(refFile);  
                    }                                             
                }

            }            
            refFileList.Clear();
            refFileList = tempRefFilelist;
        }

        public static void FixRefFilePath(ref ArrayList refFileList, ref String htmlFile)
        {
            String temp = htmlFile;
            String result = temp;

            for (int i = 0; i < refFileList.Count; i++)
            {
                RefFile refFile = (RefFile)refFileList[i];

                if (htmlFile.IndexOf(refFile.sourceUrl) >= 0)
                {
                    result = "";
                    result = temp.Replace(refFile.sourceUrl, refFile.destPath);
                    temp = result;
                }
            }
            htmlFile = result;
        }

        public static void DownloadRefFiles(ArrayList refFileList, String fullDirectory)
        {
            foreach (RefFile refFile in refFileList)
            {
                if (IsNeedFixPathAndDownload(refFile.ext))
                {
                    try
                    {
                        WebClient client = new WebClient();
                        Byte[] imgbyte = client.DownloadData(refFile.sourceFullUrl);
                        CommonUtils.SaveFile(fullDirectory, refFile.fileName, imgbyte);
                    }
                    catch (Exception e)
                    {
                        // handle exception
                        String error = e.Message;
                    }
                }
            }

        }
        public static void DownloadHtmlFile(String htmlFileName, String fullDirectory, String content)
        {
            CommonUtils.SaveFile(fullDirectory, htmlFileName, content);
        }
    }
}